#include <gcontact.h>
#include <QDebug>
#include <parse.h>

#include <QCategoryManager>

GoogleContact::GoogleContact() :
  QContact() 
{
}


GoogleContact::GoogleContact(QDomNode& node, QMap<QString,QString> groupmap) :
  QContact() 
{
  google(node,groupmap);

}

GoogleContact::GoogleContact(QContact& contact):
  QContact(contact)
{
}

void GoogleContact::google(QDomNode& node,QMap<QString,QString> groupmap)
{
for(QDomNode nn = node.firstChild(); !nn.isNull(); nn = nn.nextSibling()) {
        QDomElement ee = nn.toElement();
        QCategoryManager cats("Address Book");

        // parsing 
        QString tag  = ee.tagName();
        QString text = ee.text();

        if (tag == "email") {
          QString address = ee.attribute("address");
          
          QStringList el = emailList();
          if (! el.contains( address  ) ) {
            qDebug() << "adding address" << address;
            insertEmail( address );
          } else {
            qDebug() << "skippind address" << address;
          }

        } else if (tag == "phoneNumber") {

          QString gtype = ee.attribute("label") ;

          QMap<QContact::PhoneType, QString> nums = phoneNumbers(); // FIXME

          if (! nums.values().contains( text ) ) {
            setPhoneNumber(typeMap(gtype),  text);
            qDebug () << "adding phone" << text << "of type" << typeMap(gtype) << gtype;
          } else {

             QMapIterator<QContact::PhoneType, QString> it(nums);
             while (it.hasNext()) {
                    it.next();
                    if (it.value() == text && it.key() != typeMap(gtype) ) {
                      nums.remove(it.key() );
                      nums.remove(typeMap(gtype)); // FIXME
                      nums.insert(typeMap(gtype),  text);
                      setPhoneNumbers(nums);
                      qDebug() << "replaced phone of type" <<  typeMap(gtype) << gtype << ": " << text;
                      break;
                    }
             }
            qDebug () << "skipping phone" << text << gtype << typeMap(gtype);

          }


        } else if (tag == "groupMembershipInfo") {
          QString googleGroupId   = ee.attribute("href");
          QString googleGroupName = groupmap[googleGroupId];
          QString googleGroupQId   = "category." + googleGroupName;


          qDebug() << "Group id" <<googleGroupId << "name" << googleGroupName ;

          if (googleGroupName.isEmpty())
            continue;

          if (!cats.exists( googleGroupQId  ) )
            cats.ensureSystemCategory(googleGroupQId, googleGroupName);



          QList<QString> googleGroupList = categories();
          if (! googleGroupList.contains(googleGroupQId) )  {
            googleGroupList << googleGroupQId;

            qDebug() << "Adding group" << googleGroupName ;

            setCategories(googleGroupList);

          } else {
            qDebug() << "Skipping group" << googleGroupName;
          }

          pruneDeadCategories(googleGroupList);

          qDebug() << "group count" << googleGroupList.count();
          

        } else if (tag == "link") {
          // TODO

          QString type = ee.attribute("type");
          QString href = ee.attribute("href");

          if (type == "image/*")
            qDebug() << "image here: " << href;
          //else
          //  qDebug() << "link:" << ee.attribute("rel") << type << href;


        } else if (tag == "id") {
          // not used
          qDebug() << "Id" << text << label().isEmpty();
          //if (! label().isEmpty() )
            //idmap.insert( label(), text );

        } else if (tag == "title") {
          // handled in parser
          continue;
        }
        else {
          if (! text.isEmpty() )
            qDebug() << "unhandled tag" << tag <<  text ;  
        }
}
};

GoogleContact::~GoogleContact() 
{
};

#define in(s) ( name.contains(s)  || name.contains(tr(s)) )
QContact::PhoneType GoogleContact::typeMap(const QString & name) {
  
  if (in("Mobile") && in("Personal")) // Personal / Mobile -> 257
      return QContact::HomeMobile ;

  else if (in("Mobile") && in("Work")) // Work / Mobile -> 258
      return QContact::BusinessMobile ;

  else if in("Mobile") // Mobile -> 256
    return QContact::Mobile;

  else if  ( in("Personal") || in("Home") )// Personal -> 01
    return QContact::HomePhone;
  else if in("Work") // Work -> 02
    return QContact::BusinessPhone;
  else // 00
    return QContact::OtherPhone;
#if 0
  tr("Mobile");
  tr("Home");
  tr("Work");
  tr("Personal");
#endif
}

